
#include	"SysConfig.h"
#include	"Define.h"
#include	"Audio.h"
#include	"UI_Main.h"
#include	"Model_Option.h"
#include	"Display.h"
#include	"Key.h"
#include	"Port.h"

#ifdef  AV_CONTROLLER
#include	"AVController.h"
#endif

#ifdef  BLUETOOTH
#include	"Bluetooth.h"
#ifdef  PHONE_BOOK
#include	"PhoneBook.h"
#endif
#endif

extern unsigned char ucDISP_ScrollEnable;
extern void DISP_SetBlink(void);
extern void DISP_SetTempDisplayMode_6Sec(UI_DISP ucDes);

extern void AUDIO_SetMute(unsigned char ucDes);

UI_MENU_ID ucMenuID;
UI_MENU_ID ucMenuID_BackUp;
unsigned char ucMENU_SetHour;
unsigned char ucMENU_SetMin;
unsigned short usMENU_Timer100msec;
//unsigned char MENU_GetID(void);

extern unsigned char ucRTC_Hour;
extern unsigned char ucRTC_Min;
extern unsigned char ucRTC_Sec;

extern void AUDIO_ChangeBass(unsigned char ucDes);
extern void AUDIO_ChangeTreble(unsigned char ucDes);
extern void AUDIO_ChangeMiddle(unsigned char ucDes);
extern void AUDIO_ChangeFader(unsigned char ucDes);
extern void AUDIO_ChangeBalance(unsigned char ucDes);
extern void AUDIO_ChangeEqualizer(unsigned char ucDes);
extern void AUDIO_ChangeBeepMode(void);

#ifdef  BLUETOOTH
extern unsigned char ucBT_DeleteRequestIndex;
extern unsigned char ucBT_DeviceCnt;
extern unsigned char ucBT_Function_Active;
extern unsigned char ucDISP_BluetoothName[BT_DEVICE_NAME_LENGTH];
extern BT_DEVICE	sBT_BDList[BT_DEVICE_NUM];

#ifdef  PHONE_BOOK
extern unsigned char ucPBAP_CallHistoryCnt;
#endif

extern void BT_BluetoothVolume(unsigned char ucDes, unsigned char ucOption);
extern void BT_PBAP_SetDownload(unsigned char ucDes);
extern void BT_BluetoothOnOff(unsigned char ucDes);
extern void BT_PairingRequest(void);

extern unsigned char BT_ConnectRequest(unsigned char ucOrder);
extern void BT_DeleteRequest(unsigned char ucOrder);


extern unsigned char BT_HF_ConnectStatus(void);
extern unsigned char BT_NumberDial_CallHistory(void);

#ifdef MOVON
extern void MV_TX_HF_NumberDial(unsigned char *ucNum, unsigned char ucCnt);

#endif

#endif

void Variable_Initialize(void)
{
	ucDISP_ScrollEnable = 1;
}
void MENU_ScrollSet(void)
{
	if  (ucDISP_ScrollEnable == 1)
	{
		ucDISP_ScrollEnable = 0;
	}
	else
	{
		ucDISP_ScrollEnable = 1;
	}
}

void MENU_SetID(UI_MENU_ID ucID)
{
#ifdef  BLUETOOTH
	if  (ucBT_Function_Active == 0)
	{
		switch  (ucID)
		{
			case MENU_BT_PAIR_ID:
			case MENU_BT_SEL_ID:
			case MENU_BT_DEL_ID:
			case MENU_BT_VOL_ID:
			case MENU_BT_PB_ID:
			case MENU_BT_MODE_ID:
				ucID = MENU_BT_MODE_ID;
				break;
		}
	}
#endif
	ucMenuID = ucID;
	usMENU_Timer100msec = 5000/100;
	DISP_ClearTempDisplayMode();

	if  (ucMenuID > MENU_NONE_BACKUP)
	{
		ucMenuID_BackUp = ucMenuID;
	}

#ifdef  BLUETOOTH
	if  (ucID == MENU_BT_CONNETING_ID)
	{
		usMENU_Timer100msec = 60000/100;
	}
	if  (ucID == MENU_BT_PAIR_ACTIVE_ID)
	{
		usMENU_Timer100msec = 60000/100;
	}
	if  (ucID == MENU_BT_RECENT_ID)
	{
		usMENU_Timer100msec = 8000/100;
	}
#endif
}

void MENU_SetID_10Sec(UI_MENU_ID ucID)
{
	MENU_SetID(ucID);
	usMENU_Timer100msec = 10000/100;
}

void MENU_Clear(void)
{
	MENU_SetID(MENU_NONE);
}

unsigned char MENU_GetID(void)
{
	unsigned char ucdata;
	ucdata = ucMenuID;
	
	return(ucdata);
}
#ifdef  BLUETOOTH
void MENU_Clear_RecentCallID(void)
{
	if  (MENU_GetID() == MENU_BT_RECENT_ID)
	{
		MENU_SetID(MENU_NONE);
	}
}
#endif

unsigned char ucMENU_CurNo;

unsigned char MENU_PowerShortKey(void)
{
	unsigned char ucRet = 0;
	
	switch  (ucMenuID)
	{
	case MENU_NONE:
		break;
	case MENU_CLOCK_SET_HOUR_ID:
	case MENU_CLOCK_SET_MIN_ID:
		ucRet = 0;
		break;
#ifdef  BLUETOOTH
	case MENU_VOLUME_ID:
		if  (ucBT_Function_Active == 1)
		{
			MENU_SetID(MENU_BT_VOL_ID);
			ucRet = 1;
		}
		break;
	case MENU_BT_RECENT_ID:
		if  (MENU_GetID() == MENU_BT_RECENT_ID)
		{		
			if  (ucMENU_CurNo == ucPBAP_CallHistoryCnt)
			{
				MENU_SetID(MENU_NONE);
				ucRet = 1;
			}
			else
			{
				if  (BT_NumberDial_CallHistory() == 1)
				{
					MENU_SetID(MENU_BT_RECENT_ID);
					ucRet = 1;
				}
			}
		}		
		break;
	case MENU_BT_PAIR_ID:
		if  (ucBT_DeviceCnt == BT_DEVICE_NUM)
		{
			MENU_SetID_10Sec(MENU_BT_DEL_LIST_ID);
			DISP_SetTempDisplayMode(DISP_BT_LIST_FULL);
		}
		else
		{
			BT_PairingRequest();
			MENU_SetID(MENU_BT_PAIR_ACTIVE_ID);
		}
		ucRet = 1;
		break;
	case MENU_BT_PAIR_ACTIVE_ID:
		ucRet = 0;
		break;
	case MENU_BT_SEL_ID:
		if  (ucBT_DeviceCnt == 0)
		{
			MENU_SetID(MENU_BT_SEL_ID);
			DISP_SetTempDisplayMode(DISP_BT_NOLIST);
		}
		else
		{
			MENU_SetID(MENU_BT_SEL_LIST_ID);
			ucMENU_CurNo = 0;
		}
		ucRet = 1;
		break;
	case MENU_BT_SEL_LIST_ID:
		if  (ucBT_DeviceCnt == ucMENU_CurNo)
		{
			MENU_SetID(MENU_BT_SEL_ID);	// Previous
		}		
		else
		{
			if  (BT_ConnectRequest(ucMENU_CurNo) == 1)
			{
				MENU_SetID(MENU_BT_CONNETING_ID);
			}
		}
		ucRet = 1;
		break;
	case MENU_BT_DEL_ID:
		if  (ucBT_DeviceCnt == 0)
		{
			MENU_SetID(MENU_BT_DEL_ID);
			DISP_SetTempDisplayMode(DISP_BT_NOLIST);
		}
		else
		{
			MENU_SetID(MENU_BT_DEL_LIST_ID);
			ucMENU_CurNo = 0;
		}
		ucRet = 1;
		break;
	case MENU_BT_DEL_LIST_ID:		
		if  (ucBT_DeviceCnt == ucMENU_CurNo)
		{
			MENU_SetID(MENU_BT_DEL_ID);
		}		
		else			
		{
			BT_DeleteRequest(ucMENU_CurNo);
			if (ucBT_DeviceCnt == 1)
			{
				MENU_SetID(MENU_NONE);
				DISP_SetTempDisplayMode(DISP_BT_NOLIST);
			}	
			else
			{
				memcpy(&ucDISP_BluetoothName[0],&sBT_BDList[ucBT_DeleteRequestIndex].ucName[0],BT_DEVICE_NAME_LENGTH);
				MENU_SetID(MENU_BT_DEL_LIST_ID);
				DISP_SetTempDisplayMode(DISP_BT_ICON_DEVICENAME);	
			}
		}
		ucRet = 1;
		break;
	case MENU_BT_VOL_ID:
		MENU_SetID(MENU_BT_VOL_ACTIVE_ID);
		ucRet = 1;
		break;
	case MENU_BT_VOL_ACTIVE_ID:
		break;
	case MENU_BT_PB_ID:
		MENU_SetID(MENU_BT_PB_ACTIVE_ID);
		ucMENU_CurNo = 0;
		ucRet = 1;
		break;
	case MENU_BT_PB_ACTIVE_ID:
		if  (ucMENU_CurNo == 0)	// New Download
		{
		//	MENU_SetID(MENU_BT_PB_ACTIVE_DOWNLOAD_ID);
			if  (BT_HF_ConnectStatus() == 1)
			{
				MENU_SetID(MENU_NONE);
				DISP_SetTempDisplayMode_6Sec(DISP_BT_PHONEBOOK_DOWNLOAD_START);
				BT_PBAP_SetDownload(0);
			}
			else
			{
				//
			}
		}
		if  (ucMENU_CurNo == 1)	// Delete
		{
		//	MENU_SetID(MENU_BT_PB_ACTIVE_DELETE_ID);
			MENU_SetID(MENU_NONE);
			DISP_SetTempDisplayMode(DISP_BT_PHONEBOOK_DELETE);
			BT_PBAP_SetDownload(3);
		}
		if  (ucMENU_CurNo == 2)	// Previous
		{
			MENU_SetID(MENU_BT_PB_ID);
		}
		ucRet = 1;
		break;
	case MENU_BT_MODE_ID:
		MENU_SetID(MENU_BT_MODE_ACTIVE_ID);
		ucMENU_CurNo = 0;
		ucRet = 1;
		break;
	case MENU_BT_MODE_ACTIVE_ID:
		MENU_SetID(MENU_VOLUME_ID);
		if  (ucMENU_CurNo == 0)
		{
			if  (ucBT_Function_Active == 0)
			{
				BT_BluetoothOnOff(1);
				DISP_SetTempDisplayMode(DISP_BT_MODE_SET);
			}
		}
		else
		{
			if  (ucBT_Function_Active == 1)
			{
				BT_BluetoothOnOff(0);// Bt Off
				ucBT_Function_Active = 0;
				DISP_SetTempDisplayMode(DISP_BT_MODE_SET);
			}
		}
		ucRet = 1;
		break;
#endif
	}
	
	return(ucRet);
}

unsigned char MENU_UpDownKey(unsigned char ucDes)
{
	unsigned char ucRet = 0;
	
	switch  (ucMenuID)
	{
		default:
			break;
		case MENU_CLOCK_SET_HOUR_ID:
			if  (ucDes == 1)
			{
				if  (++ucMENU_SetHour > 23)
				{
					ucMENU_SetHour = 0;
				}
			}
			else
			{
				if  (ucMENU_SetHour == 0)
				{
					ucMENU_SetHour = 23;
				}
				else
				{
					--ucMENU_SetHour;
				}
			}
			MENU_SetID(MENU_CLOCK_SET_HOUR_ID);
			DISP_SetBlink();
			ucRet = 1;
			break;
		case MENU_CLOCK_SET_MIN_ID:
			if  (ucDes == 1)
			{
				if  (++ucMENU_SetMin > 59)
				{
					ucMENU_SetMin = 0;
				}
			}
			else
			{
				if  (ucMENU_SetMin == 0)
				{
					ucMENU_SetMin = 59;
				}
				else
				{
					--ucMENU_SetMin;
				}
			}
			MENU_SetID(MENU_CLOCK_SET_MIN_ID);
			DISP_SetBlink();
			ucRet = 1;
			break;
		case MENU_BASS_ID:
			AUDIO_ChangeBass(ucDes);
			MENU_SetID(MENU_BASS_ID);
			ucRet = 1;
			break;
		case MENU_MIDDLE_ID:
			AUDIO_ChangeMiddle(ucDes);
			MENU_SetID(MENU_MIDDLE_ID);
			ucRet = 1;
			break;
		case MENU_TREBLE_ID:
			AUDIO_ChangeTreble(ucDes);
			MENU_SetID(MENU_TREBLE_ID);
			ucRet = 1;
			break;
		case MENU_FAD_ID:
			AUDIO_ChangeFader(ucDes);
			MENU_SetID(MENU_FAD_ID);
			ucRet = 1;
			break;
		case MENU_BAL_ID:
			AUDIO_ChangeBalance(ucDes);
			MENU_SetID(MENU_BAL_ID);
			ucRet = 1;
			break;
		case MENU_LOUDNESS_ID:
			AUDIO_ChangeLoud();
			MENU_SetID(MENU_LOUDNESS_ID);
			ucRet = 1;
			break;
		case MENU_EQ_ID:
			AUDIO_ChangeEqualizer(ucDes);
			MENU_SetID(MENU_EQ_ID);
			ucRet = 1;
			break;
		case MENU_BEEP_ID:
			AUDIO_ChangeBeepMode();
			MENU_SetID(MENU_BEEP_ID);
			ucRet = 1;
			break;
		case MENU_SCROLL_ID:
			MENU_SetID(MENU_SCROLL_ID);
			MENU_ScrollSet();
			ucRet = 1;
			break;
	#ifdef  BLUETOOTH
		case MENU_BT_PAIR_ID:
		case MENU_BT_SEL_ID:
		case MENU_BT_DEL_ID:
		case MENU_BT_VOL_ID:
		case MENU_BT_PB_ID:
		case MENU_BT_MODE_ID:
			if  (ucBT_Function_Active == 1)
			{
				if  (ucDes == 1)
				{
					if  (++ucMenuID > MENU_BT_MODE_ID)
					{
						ucMenuID = MENU_BT_PAIR_ID;
					}
					if  ((BT_HF_ConnectStatus() == 0) && (ucMenuID == MENU_BT_PB_ID))
					{
						ucMenuID = MENU_BT_MODE_ID;
					}
				}
				else
				{
					if  (--ucMenuID < MENU_BT_PAIR_ID)
					{
						ucMenuID = MENU_BT_MODE_ID;
					}
					if  ((BT_HF_ConnectStatus() == 0) && (ucMenuID == MENU_BT_PB_ID))
					{
						ucMenuID = MENU_BT_VOL_ID;
					}
				}
			}
			MENU_SetID(ucMenuID);
			ucRet = 1;
			break;
		case MENU_BT_SEL_LIST_ID:
		case MENU_BT_DEL_LIST_ID:
			if  (ucDes == 1)
			{
				if  (++ucMENU_CurNo > (ucBT_DeviceCnt))	// with in Previous string
				{
					ucMENU_CurNo = 0;
				}
				
			}
			else
			{
				if  (ucMENU_CurNo == 0)
				{
					ucMENU_CurNo = ucBT_DeviceCnt;
				}
				else
				{
					--ucMENU_CurNo;
				}
			}
			MENU_SetID(ucMenuID);
			ucRet = 1;
			break;
		case MENU_BT_VOL_ACTIVE_ID:
			BT_BluetoothVolume(ucDes, 0);
			MENU_SetID(MENU_BT_VOL_ACTIVE_ID);
			ucRet = 1;
			break;
		case MENU_BT_PB_ACTIVE_ID:
			if  (ucDes == 1)
			{
				if  (++ucMENU_CurNo > 2)
				{
					ucMENU_CurNo = 2;
				}
			}
			else
			{
				if  (ucMENU_CurNo != 0)
				{
					--ucMENU_CurNo;
				}
			}
			MENU_SetID(MENU_BT_PB_ACTIVE_ID);
			ucRet = 1;
			break;
		case MENU_BT_MODE_ACTIVE_ID:
			if  (ucDes == 1)
			{
				if  (++ucMENU_CurNo > 2)
				{
					ucMENU_CurNo = 2;
				}
			}
			else
			{
				if  (ucMENU_CurNo != 0)
				{
					--ucMENU_CurNo;
				}
			}			
			MENU_SetID(MENU_BT_MODE_ACTIVE_ID);
			ucRet = 1;
			break;
		case MENU_BT_RECENT_ID:
			if  (ucDes == 1)
			{
				if  (++ucMENU_CurNo > ucPBAP_CallHistoryCnt)
				{
					ucMENU_CurNo = 20;
				}
			}
			else
			{
				if  (ucMENU_CurNo != 0)
				{
					--ucMENU_CurNo;
				}
			}
			MENU_SetID(MENU_BT_RECENT_ID);
			ucRet = 1;
			break;
			
	#endif
	}
	
	return(ucRet);
}

unsigned char MENU_MenuShortKey(void)
{
	unsigned char ucRet = 0;
	
	switch  (ucMenuID)
	{
	#ifdef  BLUETOOTH
		case MENU_BT_PAIR_ACTIVE_ID:
		case MENU_BT_CONNETING_ID:
		case MENU_BT_SEL_LIST_ID:
		case MENU_BT_DEL_LIST_ID:
		case MENU_BT_VOL_ACTIVE_ID:
		case MENU_BT_PB_ACTIVE_ID:
		case MENU_BT_PB_ACTIVE_DOWNLOAD_ID:
		case MENU_BT_PB_ACTIVE_DELETE_ID:
		case MENU_BT_MODE_ACTIVE_ID:	
	#endif
		case MENU_NONE:	
			if  (ucMenuID_BackUp == MENU_NONE)
			{
				ucMenuID_BackUp = MENU_BASS_ID;
				AUDIO_SetMute(0);
			}
		
			if  (ucMenuID_BackUp == MENU_FAD_ID)
			{
				if  (AUDIO_GetSpeakerMode() != DSPK_ALL)
				{
					ucMenuID_BackUp = MENU_BASS_ID;
				}
				if  (AVC_GetDriverSpeakerMode() == AVC_DRV_ON)
				{
					ucMenuID_BackUp = MENU_BASS_ID;
				}
			}
			MENU_SetID(ucMenuID_BackUp);
			ucRet = 1;
			break;
		case MENU_VOLUME_ID:
			MENU_SetID(MENU_BASS_ID);
			ucRet = 1;
			break;
		case MENU_BASS_ID:
			MENU_SetID(MENU_MIDDLE_ID);
			ucRet = 1;
			break;
		case MENU_MIDDLE_ID:
			MENU_SetID(MENU_TREBLE_ID);
			ucRet = 1;
			break;
		case MENU_TREBLE_ID:
		#ifdef  MODEL_ARA5040
			if  (OPT_GetOption(OPT_MASK_FAD))
			{
				MENU_SetID(MENU_FAD_ID);
			}
			else
			{
				MENU_SetID(MENU_BAL_ID);
			}
		#else
	//		if  (AUDIO_GetSpeakerMode() == DSPK_ALL)
	//		{
	//			MENU_SetID(MENU_FAD_ID);
	//		}
	//		else
	//		{
	//			MENU_SetID(MENU_BAL_ID);
	//		}
			MENU_SetID(MENU_FAD_ID);

			if  (AUDIO_GetSpeakerMode() != DSPK_ALL)
			{
				MENU_SetID(MENU_BAL_ID);
			}
			if  (AVC_GetDriverSpeakerMode() == AVC_DRV_ON)
			{
				MENU_SetID(MENU_BAL_ID);
			}
		#endif
			ucRet = 1;
			break;
		case MENU_FAD_ID:
			MENU_SetID(MENU_BAL_ID);
			ucRet = 1;
			break;
		case MENU_BAL_ID:
			MENU_SetID(MENU_LOUDNESS_ID);
			ucRet = 1;
			break;
		case MENU_LOUDNESS_ID:
			MENU_SetID(MENU_EQ_ID);
			ucRet = 1;
			break;
		case MENU_EQ_ID:
			MENU_SetID(MENU_BEEP_ID);
			ucRet = 1;
			break;
		case MENU_BEEP_ID:
			MENU_SetID(MENU_SCROLL_ID);
			ucRet = 1;
			break;

	#ifdef  BLUETOOTH
		case MENU_SCROLL_ID:
			if  (OPT_GetOption(OPT_MASK_BT) == 1)
			{
				if  (ucBT_Function_Active == 1)
				{
					MENU_SetID(MENU_BT_PAIR_ID);
				}
				else
				{
					MENU_SetID(MENU_BT_MODE_ID);
				}
			}
			else
			{
				MENU_SetID(MENU_VOLUME_ID);
			}
			ucRet = 1;
			break;
		case MENU_BT_MODE_ID:
		case MENU_BT_PAIR_ID:
		case MENU_BT_SEL_ID:
		case MENU_BT_DEL_ID:
		case MENU_BT_VOL_ID:
		case MENU_BT_PB_ID:
			MENU_SetID(MENU_VOLUME_ID);
			ucRet = 1;
			break;
		case MENU_BT_RECENT_ID:
			MENU_SetID(MENU_BASS_ID);
			ucRet = 1;	
			break;
	#else
		case MENU_SCROLL_ID:
			MENU_SetID(MENU_VOLUME_ID);
			ucRet = 1;
			break;
	#endif
		case MENU_CLOCK_SET_HOUR_ID:
			MENU_SetID(MENU_CLOCK_SET_MIN_ID);
			ucRet = 1;
			break;
		case MENU_CLOCK_SET_MIN_ID:
			MENU_SetID(MENU_CLOCK_SET_HOUR_ID);
			ucRet = 1;
			break;
	}
	
	return(ucRet);
}

unsigned char MENU_MenuLongKey(void)
{
	unsigned char ucRet = 0;
	
	switch	(ucMenuID)
	{
		case MENU_NONE:
			MENU_SetID(MENU_CLOCK_SET_HOUR_ID);
			ucMENU_SetHour = ucRTC_Hour;
			ucMENU_SetMin = ucRTC_Min;
			ucRet = 1;
			AUDIO_BeepOutStart();
			break;
		case MENU_CLOCK_SET_HOUR_ID:
		case MENU_CLOCK_SET_MIN_ID:
			ta1s = 0;	// Timer Count Stop
			ucRTC_Hour = ucMENU_SetHour;
			ucRTC_Min = ucMENU_SetMin;
			ucRTC_Sec = 0;
			MENU_SetID(MENU_NONE);
			ucRet = 1;
			ta1s = 1;	// Timer Count Start
			AUDIO_BeepOutStart();
			break;
	}
	return (ucRet);
}


/*/////////////////////////////////////////////////////////////////////////
Title : unsigned char UI_MENU_VariableCtrl(UI_KEY_ID ucKey, unsigned char ucLong)
Return 
	0 : nothing
	1 : work somehing
//////////////////////////////////////////////////////////////////////////*/
unsigned char MENU_KeyMain(KEY_ID ucKey, unsigned char ucLong)
{
	unsigned char ucRet = 0;

	switch  (ucKey)
	{
		case KEY_MENU:
			if  (ucLong == 0)
			{
				ucRet = MENU_MenuShortKey();
			}
			else
			{
				ucRet = MENU_MenuLongKey();
			}
			break;
		case KEY_VOL_UP:
			ucRet = MENU_UpDownKey(1);
			break;
		case KEY_VOL_DOWN:
			ucRet = MENU_UpDownKey(0);		
			break;
		case KEY_POWER:
			ucRet = MENU_PowerShortKey();
			break;
	}
	
	return(ucRet);
}


